﻿using System;
using System.ComponentModel.Design.Serialization;
using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;

namespace Draco.OWF.Designer.Gui
{
	/// <summary>
    /// Interaction logic for GeneralOptionsPanel.xaml
	/// </summary>
	public partial class GeneralOptionsPanel : OptionPanel
	{
        public GeneralOptionsPanel()
		{
			InitializeComponent();
            this.Txt.Text = GetPropertySafe<String>(Constants.KEY_OWF_ENGINE_ENV_ROOT_PATH, Constants.OWF_ENGINE_ENV_ROOT_PATH_DEFAULT);
		}
		
		static T GetPropertySafe<T>(string name, T defaultValue)
		{
			if (PropertyService.Contains(name)) {
				return PropertyService.Get<T>(name, defaultValue);
			} else {
				return defaultValue;
			}
		}
		
		public override bool SaveOptions()
		{
            String path = this.Txt.Text;
            if (String.IsNullOrWhiteSpace(path) || !Directory.Exists(path))
            {
                path = Constants.OWF_ENGINE_ENV_ROOT_PATH_DEFAULT;
            }
            PropertyService.Set(Constants.KEY_OWF_ENGINE_ENV_ROOT_PATH,path);

			return true;
		}

        private void Btn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //FolderBrowserDialog fbd = new FolderBrowserDialog();
            //DialogResult result = fbd.ShowDialog();

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".exe";
            dlg.Filter = "exe Files (*.exe)|*.exe";

            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                this.Txt.Text = Path.GetDirectoryName(filename);
            }
        }
	}
}
